<!-- 仓库材料关系编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="storeMaterialModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="storeMaterialModalApp.storeMaterial"
        :rules="storeMaterialModalApp.rules"
        :label-col="{md: {span: 12}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 12}, sm: {span: 24}}">
        <a-row :gutter="16">

          <a-col :lg="10" :md="12" :sm="24" :xs="24">
            <a-form-item label="原材料名称" name="name">
              <m-entity-select
                v-model:value="storeMaterialModalApp.storeMaterial.name"
                placeholder="请选择原材料"
                module="material"
                entity="Material"
                show-options-name="name"
                class="ele-fluid"  @getEntityMaterialSelect="getEntityMaterialSelect"
              />
            </a-form-item>
          </a-col>

          <a-col :lg="10" :md="12" :sm="24" :xs="24">
            <a-form-item label="数量(kg):" name="quantity">
              <a-input-number
                v-model:value="storeMaterialModalApp.storeMaterial.quantity"
                placeholder="请输入数量"
                :min="0"
                :step="0.5"
                class="ele-fluid importanceI"></a-input-number>
            </a-form-item>
          </a-col>

          <a-col :lg="10" :md="12" :sm="24" :xs="24">
            <a-form-item label="单价(元):" name="price">
              <a-input-number
                v-model:value="storeMaterialModalApp.storeMaterial.price"
                placeholder="请输入单价"
                :min="0"
                :step="0.5"
                class="ele-fluid importanceI"></a-input-number>
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="storeMaterialModalApp.display">
                <teleport to="#storeMaterialEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="storeMaterialListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(storeMaterialModalApp.storeMaterial)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!storeMaterialModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(storeMaterialModalApp.storeMaterial)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, ref, onMounted} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {StoreMaterialService} from "@/views/restaurant/material/storeMaterial/storeMaterialService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';
import MEntitySelect from "@/components/MEntity/entitySelect";



export default defineComponent({
  components: {MEntitySelect},
  emits:['editClose'],
  setup(props,{emit}) {
    // 省市区数据
    const form2 = ref({})
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let storeMaterialListApp = inject('storeMaterialListApp', reactive({}));
    const storeMaterialModalApp = reactive({
      id: null,
      storeMaterial: {},
      isEdit: false,
      display: false
    });
    storeMaterialModalApp.rules = {

      materialName: [
        {
          required: true,
          type: 'string',
          message: '请输入材料名称',
          trigger: 'blur'
        }
      ],
      quantity: [
        {
          required: true,
          type: 'number',
          message: '请输入数量',
          trigger: 'blur'
        }
      ],
      price: [
        {
          required: true,
          type: 'number',
          message: '请输入单价',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      storeMaterialModalApp.storeMaterial = reactive({});
      if (routeId) {//路由
        storeMaterialModalApp.id = routeId;
        storeMaterialModalApp.display = false;
        findStoreMaterial(storeMaterialModalApp.id);
      } else if (storeMaterialListApp.editModalForEdit) {//修改弹窗
        storeMaterialModalApp.id = storeMaterialListApp.currentId;
        storeMaterialModalApp.display = true;
      } else if (!storeMaterialListApp.editModalForEdit && storeMaterialListApp.addModalForEdit) {//新增弹窗
        storeMaterialModalApp.display = true;
      } else {
        storeMaterialModalApp.title = "新增材料";
      }
      if (storeMaterialModalApp.id) findStoreMaterial(storeMaterialModalApp.id);
    })

    const findStoreMaterial = (id) => {
      StoreMaterialService.findStoreMaterialForView(id).then((res) => {
        console.log(res)

        storeMaterialModalApp.storeMaterial = res.data;
        //TODO:为编辑准备.
        storeMaterialModalApp.title = "编辑材料 " + storeMaterialModalApp.storeMaterial.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找材料出错，原因是：${error.message}`, status: 'error'});
      })
    }
    const getEntityMaterialSelect = (data) =>{
      storeMaterialModalApp.storeMaterial.materialId = data.eid
      storeMaterialModalApp.storeMaterial.name = data.name
      // console.log(storeMaterialModalApp.storeMaterial.materialId)
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      console.log(data)
        form2.value.validate().then(()=> {
          //当为数组时用逗号连接
          if (data.eid) {
            //修改
            StoreMaterialService.updateStoreMaterial(1,data).then(() => {
              storeMaterialListApp.editModalShowing=false;
              // console.log(storeMaterialListApp.editModalShowing);
              emit('editClose',true);
              VXETable.modal.message({content: '操作成功', status: 'success'});
              // setTimeout(onBack, 3000);
            }).then(()=>{
              VXETable.modal.message({content: '操作成功', status: 'success'});
              console.log(storeMaterialListApp.editModalShowing)
            }).catch(error => {
              VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
            })
          } else {
            //新增
            StoreMaterialService.saveStoreMaterial(1,data).then((res) => {
              console.log(res);
              storeMaterialListApp.editModalShowing=false;
              emit('editClose',true);
              VXETable.modal.message({content: '操作成功', status: 'success'});
              // setTimeout(onBack, 3000);
            }).catch(error => {
              VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
            })
          }
        })

    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/restaurant/material/storeMaterial`);
      store.dispatch('user/tabRemove', route.fullPath);
    }
    return {
      cityData,
      routeId,
      form2,
      storeMaterialListApp,
      storeMaterialModalApp,
      onSubmit,
      continueSubmit,
      onBack,
      getEntityMaterialSelect
    }

  },
})
</script>

<style scoped>

</style>
